﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLNS_BLL;
using QLNS_Public;

namespace QLNS_GiaoDien
{
    public partial class uc_NhanVien : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_NhanVien()
        {
            InitializeComponent();
        }

        NhanVien_BLL nhanvien_bll = new NhanVien_BLL();
        private void uc_NhanVien_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = nhanvien_bll.LayDuLieu();
        }
        int flag;
        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = 0;
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = 1;
        }
        NhanVien_Public nv = new NhanVien_Public();
        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            nv.MaNhanVien = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Nhân Viên"));
            nv.HoTen = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Họ Và Tên").ToString();
            if (XtraMessageBox.Show("Bạn có muốn xóa thông tin nhân viên: '" + nv.HoTen + "' có Mã số là: '" + nv.MaNhanVien + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                nhanvien_bll.Delete_NhanVien(nv);
                uc_NhanVien_Load(sender,e);
            }
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                nv.HoTen = txt_HoTen.Text;
                nv.NgaySinh = Convert.ToDateTime(dateTime_NgaySinh.Text);
                nv.GioiTinh = cob_GioiTinh.Text;
                nv.TinhTrangHonNhan = cob_TinhTrangHN.Text;
                nv.CMTND = int.Parse("0" + txt_CMT.Text);
                nv.NoiSinh = txt_NoiSinh.Text;
                nv.DienThoai = txt_DienThoai.Text;
                nv.MaPhong = int.Parse(cob_MaPhong.Text);
                nv.MaChucVu = int.Parse(cob_MaChucVu.Text);
                nv.MaBacLuong = int.Parse(cob_MaBacLuong.Text);
                nv.NgayVaoLam = Convert.ToDateTime(dateTime_NgayVaoLam.Text);
                nv.LoaiHopDong = cob_HopDong.Text;
                nv.NgayKy = Convert.ToDateTime(dateTime_NgayKy.Text);
                nv.NgayHetHan = Convert.ToDateTime(dateTime_NgayHetHan.Text);
                nv.TenDangNhap = txt_TenDangNhap.Text;
                nv.MatKhau = txt_MatKhau.Text;
                nv.Maquyen = int.Parse(cob_MaQuyen.Text);
                nv.GhiChu = txt_GhiChu.Text;
                nv.TrangThai = cob_TrangThai.Text;
                nhanvien_bll.Insert_NhanVien(nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv);

                XtraMessageBox.Show("Thêm mới thông tin nhân viên thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                uc_NhanVien_Load(sender,e);
            }
            if (flag == 1)
            {
                nv.MaNhanVien = int.Parse(txt_MaNhanVien.Text);
                nv.HoTen = txt_HoTen.Text;
                nv.NgaySinh = Convert.ToDateTime(dateTime_NgaySinh.Text);
                nv.GioiTinh = cob_GioiTinh.Text;
                nv.TinhTrangHonNhan = cob_TinhTrangHN.Text;
                nv.CMTND = int.Parse("0" + txt_CMT.Text);
                nv.NoiSinh = txt_NoiSinh.Text;
                nv.DienThoai = txt_DienThoai.Text;
                nv.MaPhong = int.Parse(cob_MaPhong.Text);
                nv.MaChucVu = int.Parse(cob_MaChucVu.Text);
                nv.MaBacLuong = int.Parse(cob_MaBacLuong.Text);
                nv.NgayVaoLam = Convert.ToDateTime(dateTime_NgayVaoLam.Text);
                nv.LoaiHopDong = cob_HopDong.Text;
                nv.NgayKy = Convert.ToDateTime(dateTime_NgayKy.Text);
                nv.NgayHetHan = Convert.ToDateTime(dateTime_NgayHetHan.Text);
                nv.TenDangNhap = txt_TenDangNhap.Text;
                nv.MatKhau = txt_MatKhau.Text;
                nv.Maquyen = int.Parse(cob_MaQuyen.Text);
                nv.GhiChu = txt_GhiChu.Text;
                nv.TrangThai = cob_TrangThai.Text;
                nhanvien_bll.Update_NhanVien(nv,nv,nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv);

                XtraMessageBox.Show("Sửa thông tin nhân viên thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                uc_NhanVien_Load(sender, e);
            }
        }

        private void txt_CMT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaNhanVien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void cob_BacLuong_Click(object sender, EventArgs e)
        {
            string sql = "select * from tbl_BacLuong";
            DataTable dt=new DataTable();
            dt=nhanvien_bll.TaoBang(sql);
            cob_MaBacLuong.DataSource = dt;
            cob_MaBacLuong.DisplayMember = "MaBacLuong";
            cob_BacLuong.DataSource=dt;
            cob_BacLuong.DisplayMember = "HeSo";
        }

        private void cob_ChucVu_Click(object sender, EventArgs e)
        {
            string sql = "select * from tbl_ChucVu";
            DataTable dt = new DataTable();
            dt = nhanvien_bll.TaoBang(sql);
            cob_MaChucVu.DataSource = dt;
            cob_MaChucVu.DisplayMember = "MaChucVu";
            cob_ChucVu.DataSource=dt;
            cob_ChucVu.DisplayMember = "TenChucVu";
        }

        private void cob_PhongBan_Click(object sender, EventArgs e)
        {
            string sql = "select * from tbl_PhongBan";
            DataTable dt = new DataTable();
            dt = nhanvien_bll.TaoBang(sql);
            cob_MaPhong.DataSource = dt; ;
            cob_MaPhong.DisplayMember = "MaPhong";
            cob_PhongBan.DataSource = dt;
            cob_PhongBan.DisplayMember="TenPhong";
        }

        private void cob_Quyen_Click(object sender, EventArgs e)
        {
            string sql = "select * from tbl_Quyen";
            DataTable dt = new DataTable();
            dt = nhanvien_bll.TaoBang(sql);
            cob_MaQuyen.DataSource = dt;
            cob_MaQuyen.DisplayMember = "MaQuyen";
            cob_Quyen.DataSource = dt;
            cob_Quyen.DisplayMember = "TenQuyen";
        }

    }
}
